//
//  AlertView.m
//  Learn
//
//  Created by 迟建国 on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "AlertView.h"

@implementation AlertView

+ (AlertView *)alertWith:(UIViewController *)vc title:(NSString *)title content:(NSString *)content cancel:(NSString *)cancel sure:(NSString *)sure Complection:(void(^)(void))block {
    AlertView *  inputView;
    inputView = [[AlertView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 185)];
    inputView.vc = vc;
    [inputView initUIWithTitle:title content:content cancel:cancel sure:sure];
    inputView.complectionBlock = block;
    
    [vc gq_presentLikeAlertView:inputView];
    vc.gq_enableBlankDismiss = NO;
    
    return inputView;
}
- (void)initUIWithTitle:(NSString *)title content:(NSString *)content cancel:(NSString *)cancel sure:(NSString *)sure {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 30, 185)];
    view.userInteractionEnabled = YES;
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 20;
    view.layer.masksToBounds = YES;
    view.center = self.center;
    [self addSubview:view];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = title;
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.numberOfLines = 0;
    titleL.textColor = rgba(51, 51, 51, 1);
    titleL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:18];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(45);
        make.right.equalTo(view.mas_right).offset(-45);
        make.top.equalTo(view.mas_top).offset(30);
    }];
    if (!IsStrEmpty(content)) {
        UILabel *contentL = [[UILabel alloc] init];
        contentL.text = content;
        contentL.numberOfLines = 0;
        contentL.textAlignment = NSTextAlignmentCenter;
        contentL.textColor = rgba(51, 51, 51, 1);;
        contentL.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
        [view addSubview:contentL];
        [contentL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(15);
            make.right.equalTo(view.mas_right).offset(-15);
            make.top.equalTo(titleL.mas_bottom).offset(15);
        }];
    }
    
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:cancel forState:UIControlStateNormal];
    [cancelBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    cancelBtn.layer.cornerRadius = 20;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.borderColor = rgba(51, 51, 51, 1).CGColor;
    cancelBtn.layer.masksToBounds = YES;
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(25);
        make.bottom.equalTo(view.mas_bottom).offset(-20);
        make.height.offset(40);
        make.width.offset((SCREEN_W - 105)/2);
    }];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:sure forState:UIControlStateNormal];
    [sureBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    sureBtn.backgroundColor = rgba(41, 241, 156, 1);
    sureBtn.layer.cornerRadius = 20;
    sureBtn.layer.masksToBounds = YES;
    [sureBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.mas_right).offset(-25);
        make.bottom.equalTo(view.mas_bottom).offset(-20);
        make.height.offset(40);
        make.width.offset((SCREEN_W - 105)/2);
    }];
    
}

- (void)cancel {
    [self.vc gq_removeBgView];
}

- (void)sure {
    self.complectionBlock();
    [self.vc gq_removeBgView];
}
@end
